package com.auto.api.core.type.json.yapi;

import com.auto.api.core.constants.MappingConstants;
import com.auto.api.common.utils.JsonMapper;
import com.auto.api.core.util.BeanUtil;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.ResolvableType;

/**
 * @program:
 * @description: MapResultJsonTypeHandler类型处理器
 * @author: liangshf
 * @create: 2020-09-24
 */
public class MapApiResultJsonTypeHandler extends AbstractResultJsonTypeHandler {

    private static final Logger logger = LoggerFactory.getLogger(BeanUtil.class);

    @Override
    public boolean canCovert(ResolvableType resolvableType) {
        return resolvableType.resolve().getSimpleName().equals(MappingConstants.MAP);
    }

    @Override
    public String getResultJson(ResolvableType resolvableType, Map root){
        logger.info("resultClass is error：{}", resolvableType.resolve().getName());
        root.put(MappingConstants.MAP, BeanUtil.getProperties(MappingConstants.MAP, MappingConstants.EMPTY_STRING));
        return JsonMapper.toJson(root);
    }
}